/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Piasy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.chensf.studydemo.common;

import android.graphics.PointF;

import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

/**
 * credit: https://github.com/Piasy/BigImageViewer/issues/2
 */

public class DisplayOptimizeListener implements SubsamplingScaleImageView.OnImageEventListener {
    public static final int INIT_SCALE_TYPE_CENTER = 0;
    public static final int INIT_SCALE_TYPE_CENTER_CROP = 1;
    public static final int INIT_SCALE_TYPE_CENTER_INSIDE = 2;
    public static final int INIT_SCALE_TYPE_FIT_CENTER = 3;
    public static final int INIT_SCALE_TYPE_FIT_END = 4;
    public static final int INIT_SCALE_TYPE_FIT_START = 5;
    public static final int INIT_SCALE_TYPE_FIT_XY = 6;
    public static final int INIT_SCALE_TYPE_CUSTOM = 7;
    public static final int INIT_SCALE_TYPE_START = 8;
    public static final int INIT_SCALE_TYPE_FULLSCREEN = 9;

    private static final int LONG_IMAGE_SIZE_RATIO = 2;

    private final SubsamplingScaleImageView mImageView;

    private int mInitScaleType;

    public DisplayOptimizeListener(SubsamplingScaleImageView imageView) {
        mImageView = imageView;
    }

    @Override
    public void onReady() {
        int imageWidth = mImageView.getSWidth();
        int imageHeight = mImageView.getSHeight();
        int viewWidth = mImageView.getRootView().getWidth();
        int viewHeight = mImageView.getRootView().getHeight();

        if (mInitScaleType == INIT_SCALE_TYPE_CUSTOM
                || mInitScaleType == INIT_SCALE_TYPE_FULLSCREEN) {
            if (imageWidth > 0 && imageHeight > 0
                    && imageHeight > viewHeight
                    && (float) imageHeight / imageWidth > LONG_IMAGE_SIZE_RATIO) {
                //超长图，先计算图像放大符合容器的最佳比例
                float scale = (float) viewWidth / imageWidth;
                mImageView.setMinScale(scale);
                mImageView.setDoubleTapZoomScale(scale * 2);
                mImageView.setMaxScale(scale * 3);
                // scale at top
                mImageView.setScaleAndCenter(scale, new PointF(imageWidth / 2F, 0));
            } else {
                //先计算图像放大符合容器的最佳比例
                float scale = Math.min((float) viewWidth / imageWidth,
                        (float) viewHeight / imageHeight);
                mImageView.setMinScale(scale);
                mImageView.setDoubleTapZoomScale(scale * 2);
                mImageView.setMaxScale(scale * 3);
                mImageView.setScaleAndCenter(scale, new PointF(imageWidth / 2F, imageHeight / 2F));
            }
        }

    }

    @Override
    public void onImageLoaded() {
        //onImageLoaded
    }

    @Override
    public void onPreviewLoadError(Exception e) {
        //onPreviewLoadError
    }

    @Override
    public void onImageLoadError(Exception e) {
        //onImageLoadError
    }

    @Override
    public void onTileLoadError(Exception e) {
        //onTileLoadError
    }

    @Override
    public void onPreviewReleased() {
        //onPreviewReleased
    }

    public void setInitScaleType(int initScaleType) {
        mInitScaleType = initScaleType;
    }
}
